package ltd.hxya.novel.rabbitmq.service.impl;

import com.rabbitmq.client.Channel;
import ltd.hxya.novel.common.constant.rabbit.RabbitConstant;
import ltd.hxya.novel.common.utils.MailUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
public class ErrorServiceImpl {

    @Autowired
    private MailUtils mailUtils;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(RabbitConstant.ERROR_QUEUE),
            exchange = @Exchange(RabbitConstant.ERROR_EXCHANGE),
            key = RabbitConstant.ERROR_ROUTING_KEY))
    @RabbitHandler
    public void error( Message message, Channel channel) throws IOException {
        try {
            mailUtils.sendMail("rabbitMQ消费者调用失败",message.toString());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,true);
        }
    }
}
